package com.whosly.mysql.protocol;

import lombok.Getter;

/**
 * MYSQL field flag
 *
 *                          The possible flag values at time of
 *                          writing (taken from  include/mysql_com.h), in hexadecimal:
 *                          0001 NOT_NULL_FLAG
 *                          0002 PRI_KEY_FLAG
 *                          0004 UNIQUE_KEY_FLAG
 *                          0008 MULTIPLE_KEY_FLAG
 *                          0010 BLOB_FLAG
 *                          0020 UNSIGNED_FLAG
 *                          0040 ZEROFILL_FLAG
 *                          0080 BINARY_FLAG
 *                          0100 ENUM_FLAG
 *                          0200 AUTO_INCREMENT_FLAG
 *                          0400 TIMESTAMP_FLAG
 *                          0800 SET_FLAG
 *
 * @see   https://dev.mysql.com/doc/dev/mysql-server/latest/mysql__com_8h_source.html
 */
public enum FlagProtocolType {
    // 16进制 to 10进制
    // https://www.hexadecimaldictionary.com/hexadecimal/0x0020/

    // 特别说明: 此处 key额定义特意定义为 FALG, 与规范定义区分, 便于使用

    // 0x0001, Field can't be NULL
    NOT_NULL_FALG(1),
    // 0x0002, Field is part of a primary key
    PRI_KEY_FALG(2),
    // 0x0004, Field is part of a unique key
    UNIQUE_KEY_FALG(4),
    // 0x0008, Field is part of a key
    MULTIPLE_KEY_FALG(8),
    // 0x0010, Field is a blob
    BLOB_FALG(16),
    // 0x0020, Field is unsigned
    UNSIGNED_FALG(32),
    // 0x0040,  Field is zerofill
    ZEROFILL_FALG(64),
    // 0x0080, Field is binary
    BINARY_FALG(128),

    /* ========================================== */
    /* The following are only sent to new clients */
    /* ========================================== */
    // 0x0100, field is an enum
    ENUM_FALG(256),
    // 0x0200, field is a autoincrement field
    AUTO_INCREMENT_FALG(512),
    // 0x0400, Field is a timestamp
    TIMESTAMP_FALG(1024),
    // 0x0800, field is a set
    SET_FALG(2048),


    // Field doesn't have default value
    NO_DEFAULT_VALUE_FLAG(4096),
    // Field is set to NOW on UPDATE
    ON_UPDATE_NOW_FLAG(8192),
    // Field is num (for clients)
    NUM_FLAG(32768),



    // other
    // 0x0000, normal Field
    NORMAL(0),

    _13(13),
    ;

    /**
     * 十进制 flag
     */
    @Getter
    private int flag;

    FlagProtocolType(int flag) {
        this.flag = flag;
    }

    public static FlagProtocolType of(int flag){
        for (FlagProtocolType type : values()) {
            if(type.getFlag() == flag){
                return type;
            }
        }

        return _13;
    }

}
